<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common.jsp"%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>演示列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input userName" placeholder="姓名" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
                <a class="layui-btn layui-btn-normal add_btn" data-type="add">新增</a>
            </div>

        </form>
    </blockquote>
    <table id="demoList" lay-filter="demoList"></table>
</form>
<!--操作-->
<script type="text/html" id="demoListBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/javascript" src="${ctx}/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer', 'table', 'laytpl', 'laydate'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate = layui.laydate;


        //分页列表
        var tableIns = table.render({
            elem: '#demoList',
            url: '${ctx}/demo/page',
            cellMinWidth: 95,
            page: true,
            height: "full-125",
            loading: true,
            id: "demoListTable",
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,statusCode: 10200 //规定成功的状态码，默认：0
                ,msgName: 'message' //规定状态信息的字段名称，默认：msg
                ,countName: 'total' //规定数据总数的字段名称，默认：count
                ,dataName: 'records' //规定数据列表的字段名称，默认：data
            },
            cols: [[
                {field: 'userName', title: '昵称', minWidth: 100, align: "center"},
                {field: 'userAge', title: '年龄', minWidth: 100, align: "center"},
                {field: 'userSex', title: '年龄', minWidth: 100, align: "center", templet:function (d) {
                    if (d.userSex == 1) {
                        return "<span style='color: blue'>男</sapn>";
                    }
                    if (d.userSex == 2) {
                        return "<span style='color: pink'>女</span>";
                    }
                    if (d.userSex == 3) {
                        return "<span style='color: green'>中性</span>";
                    }

                }},

                {field: 'gmtCreate', title: '注册时间', minWidth: 100, align: "center"},
                {title: '操作', width: 360, templet: '#demoListBar', fixed: "right", align: "center"}
            ]]
        });

        //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
        $(".search_btn").on("click", function () {
            tableIns.reload({
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: { //设定异步数据接口的额外参数，任意设
                    userName: $(".userName").val()
                }
            })
        });

        //删除
        function del (demoId) {
            layui.layer.confirm("该数据将被删除,确认吗？", {
                icon: 3,
                skin: 'larry-green',
                title: "删除提示",
                offset: '200px',
                closeBtn: 0,
                skin: 'layui-layer-molv',
                anim: Math.ceil(Math.random() * 6),
                btn: ['确定', '取消']
            },function (index) {
                $.ajax({
                    url: '${ctx}/demo/deleteById',
                    data: {demoId:demoId},
                    method: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 10200) {
                            layer.msg("操作成功", {icon: 6});
                            tableIns.reload();
                        }else {
                            layer.msg(res.message, {icon: 5});
                        }
                    },
                    error: function () {
                        layer.msg("系统繁忙,请稍后再试!", {icon: 5});
                    }
                });
                layui.layer.close(index);
            })
        }

        //新增
        $(".add_btn").click(function () {
            toEdit();
        });

        //编辑
        function toEdit(demoId) {
            var index = layui.layer.open({
                title: "编辑",
                type: 2,
                content: "${ctx}/page/toEditDemo?demoId="+demoId,
                success: function () {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            })
        }

        //列表操作
        table.on('tool(demoList)', function (obj) {
            var layEvent = obj.event,
                //当前行数据
                data = obj.data;

            if (layEvent === 'edit') {
                toEdit(data.id)
            }
            if (layEvent === 'del') {
                del(data.id)
            }
        });
    })
</script>
</body>
</html>
